<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float: right"
          @click="handleSearchList('searchForm')"
          type="primary"
          size="small"
        >
          查询结果
        </el-button>
        <el-button
          style="float: right; margin-right: 15px"
          @click="handleResetSearch()"
          size="small"
        >
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form
          :inline="true"
          :model="listQuery"
          :rules="queryRules"
          ref="searchForm"
          size="small"
          label-width="140px"
        >
          <el-form-item label="输入搜索：">
            <el-input
              v-model="listQuery.title"
              placeholder="问题标题"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="问题ID：" prop="urid">
            <el-input
              v-model="listQuery.uqid"
              placeholder="问题ID"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="问题分区：">
            <el-cascader
              clearable
              v-model="selectPartitionValue"
              :options="partitionOptions"
            >
            </el-cascader>
          </el-form-item>
          <el-form-item label="发布者ID：" prop="uuid">
            <el-input
              v-model="listQuery.uuid"
              placeholder="发布者ID"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="发布者昵称：">
            <el-input
              v-model="listQuery.userNickname"
              placeholder="发布者昵称"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="提问地址：">
            <el-input
              v-model="listQuery.address"
              placeholder="提问地址"
              clearable
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
    </el-card>
    <div class="table-container">
      <el-tabs v-model="statusTab" type="card" @tab-click="handleClickTabs">
        <el-tab-pane
          :label="'全部(' + countMap.allCount + ')'"
          name="-1"
        ></el-tab-pane>
        <el-tab-pane
          :label="'待审核(先发后审)(' + countMap.issueReviewCount + ')'"
          name="10"
        ></el-tab-pane>
        <el-tab-pane
          :label="'待审核(先审后发)(' + countMap.reviewIssueCount + ')'"
          name="20"
        >
        </el-tab-pane>
        <el-tab-pane
          :label="'已下架(' + countMap.offTheShelfCount + ')'"
          name="40"
        ></el-tab-pane>
        <el-tab-pane
          :label="'审核通过(' + countMap.approvedCount + ')'"
          name="30"
        ></el-tab-pane>

        <el-tab-pane
          :label="'审核未通过(' + countMap.notPassedCount + ')'"
          name="0"
        ></el-tab-pane>
      </el-tabs>
      <el-table
        :data="list"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        v-loading="listLoading"
        border
      >
        <el-table-column
          type="selection"
          width="50"
          align="center"
        ></el-table-column>
        <el-table-column label="唯一标识" width="170" align="center">
          <template slot-scope="scope">{{ scope.row.uqid }}</template>
        </el-table-column>
        <el-table-column label="问题标题" align="center">
          <template slot-scope="scope"
            ><b
              >【{{ scope.row.partitionName }}】{{ scope.row.title }}</b
            ></template
          >
        </el-table-column>
        <el-table-column label="提问地址" width="120" align="center">
          <template slot-scope="scope">{{ scope.row.address }}</template>
        </el-table-column>
        <el-table-column label="视频作者" width="120" align="center">
          <template slot-scope="scope"> {{ scope.row.userNickname }}</template>
        </el-table-column>
        <el-table-column label="点赞量" width="70" align="center">
          <template slot-scope="scope">{{ scope.row.supportCount }}</template>
        </el-table-column>
        <el-table-column label="回答量" width="70" align="center">
          <template slot-scope="scope">{{ scope.row.answerCount }}</template>
        </el-table-column>
        <el-table-column label="浏览量" width="70" align="center">
          <template slot-scope="scope">{{ scope.row.viewCount }}</template>
        </el-table-column>
        <el-table-column label="提问时间" width="160" align="center">
          <template slot-scope="scope">
            {{ scope.row.ctime }}
          </template>
        </el-table-column>
        <el-table-column label="审核状态" width="110" align="center">
          <template slot-scope="scope">
            <p
              :class="
                scope.row.status === 30 ? 'success-status' : 'error-status'
              "
            >
              {{ scope.row.status | statusFilter }}
            </p>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleVerify(scope.$index, scope.row)"
              >审核
            </el-button>
            <el-button
              size="mini"
              type="primary"
              @click="handleViewDetail(scope.$index, scope.row)"
              >查看详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="batch-operate-container">
      <el-select size="small" v-model="operateType" placeholder="批量操作">
        <el-option
          v-for="item in operates"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
      <el-button
        style="margin-left: 20px"
        class="search-button"
        @click="handleBatchOperate()"
        type="primary"
        size="small"
      >
        确定
      </el-button>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="listQuery.pageSize"
        :page-sizes="[10, 15, 20, 50]"
        :current-page.sync="listQuery.pageNum"
        :total="total"
      >
      </el-pagination>
      <div style="height: 20px"></div>
    </div>
    <el-dialog title="审核" :visible.sync="verifyDialogVisible" width="30%">
      <el-form :model="verifyForm" label-width="80px">
        <el-form-item label="审核结果">
          <el-radio-group v-model="verifyForm.verifyStatus" size="medium">
            <el-radio :label="40">下架</el-radio>
            <el-radio :label="30">通过</el-radio>
            <el-radio :label="0">不通过</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核意见">
          <el-input
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 4 }"
            placeholder="请输入内容"
            v-model="verifyForm.detail"
          >
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleVerifySubmit" size="small"
          >确 定</el-button
        >
        <el-button @click="verifyDialogVisible = false" size="small"
          >取 消</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { fetchList, getCounts, updatVerifyStatus } from "@/api/question.js";
import { fetchListWithChildren } from "@/api/partition.js";
import { validateNumber } from "@/utils/validate.js";

const defaultListQuery = {
  uqid: null,
  uuid: null,
  userNickname: null,
  pid: null,
  title: null,
  address: null,
  status: null,
  pageNum: 1,
  pageSize: 10,
};
export default {
  name: "productList",
  data() {
    var checkNumber = (rule, value, callback) => {
      if (!value) {
        callback();
      }
      if (!validateNumber(value)) {
        callback(new Error("请输入数字值"));
      } else {
        callback();
      }
    };
    return {
      operates: [
        {
          label: "批量审核",
          value: "verify",
        },
      ],
      operateType: "verify",
      multipleSelection: [],
      listQuery: Object.assign({}, defaultListQuery),
      queryRules: {
        uqid: [{ validator: checkNumber }],
        uuid: [{ validator: checkNumber }],
      },
      selectPartitionValue: null,
      partitionOptions: [],
      list: null,
      total: null,
      listLoading: true,
      statusTab: "-1",
      countMap: {
        allCount: 0,
        offTheShelfCount: 0,
        approvedCount: 0,
        reviewIssueCount: 0,
        issueReviewCount: 0,
        notPassedCount: 0,
      },
      verifyDialogVisible: false,
      verifyForm: {
        uqids: [],
        verifyStatus: 30,
        detail: "",
      },
    };
  },
  created() {
    this.getCountMap();
    this.getList();
    this.getPartitionList();
  },
  watch: {
    selectPartitionValue: function (newValue) {
      if (newValue != null) {
        this.listQuery.pid = newValue[newValue.length - 1];
      } else {
        this.listQuery.pid = null;
      }
    },
  },
  filters: {
    statusFilter(value) {
      if (value === 40) {
        return "已下架";
      } else if (value === 30) {
        return "审核通过";
      } else if (value === 20) {
        return "待审核";
      } else if (value === 10) {
        return "待审核";
      } else {
        return "审核未通过";
      }
    },
  },
  methods: {
    getList() {
      this.listLoading = true;
      fetchList(this.listQuery).then((response) => {
        this.listLoading = false;
        this.list = response.data.list;
        this.total = response.data.total;
      });
    },
    getPartitionList() {
      fetchListWithChildren().then((response) => {
        let list = response.data;
        this.partitionOptions = [];
        for (let i = 0; i < list.length; i++) {
          let children = [];
          if (list[i].children != null && list[i].children.length > 0) {
            for (let j = 0; j < list[i].children.length; j++) {
              let cc = [];
              let item = list[i].children[j];
              if (item.children != null && item.children.length > 0) {
                for (let k = 0; k < item.children.length; k++) {
                  const element = item.children[k];
                  cc.push({
                    label: element.partitionName,
                    value: element.id,
                    info: element.info,
                  });
                }
              }
              children.push({
                label: list[i].children[j].partitionName,
                value: list[i].children[j].id,
                info: list[i].children[j].info,
                children: cc.length === 0 ? null : cc,
              });
            }
          }
          this.partitionOptions.push({
            label: list[i].partitionName,
            value: list[i].id,
            info: list[i].info,
            children: children.length === 0 ? null : children,
          });
        }
      });
    },
    getCountMap() {
      getCounts(this.listQuery).then((res) => {
        this.countMap = res.data;
      });
    },
    handleSearchList(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.listQuery.pageNum = 1;
          this.statusTab = "-1";
          this.getList();
          this.getCountMap();
        }
      });
    },
    handleClickTabs(tab, event) {
      var status = Number(this.statusTab);
      if (status !== -1) {
        this.listQuery.status = status;
      } else {
        this.listQuery.status = null;
      }
      this.getList();
      this.getCountMap();
    },
    handleSizeChange(val) {
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
      this.getCountMap();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getList();
      this.getCountMap();
    },
    handleResetSearch() {
      this.selectPartitionValue = [];
      this.listQuery = Object.assign({}, defaultListQuery);
    },
    handleViewDetail(index, row) {
      this.$router.push({
        path: "/cms/questionDetail",
        query: { uqid: row.uqid },
      });
    },
    handleVerify(index, row) {
      this.verifyDialogVisible = true;
      this.verifyForm.uqids = [row.uqid];
    },
    handleVerifySubmit() {
      let params = new URLSearchParams();
      params.append("uqids", this.verifyForm.uqids);
      params.append("verifyStatus", this.verifyForm.verifyStatus);
      params.append("detail", this.verifyForm.detail);
      updatVerifyStatus(params).then((res) => {
        this.$message({
          message: "修改成功",
          type: "success",
          duration: 1000,
        });
        this.verifyDialogVisible = false;
        this.getList();
        this.getCountMap();
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleBatchOperate() {
      if (this.operateType == null) {
        this.$message({
          message: "请选择操作类型",
          type: "warning",
          duration: 1000,
        });
        return;
      }
      if (this.multipleSelection == null || this.multipleSelection.length < 1) {
        this.$message({
          message: "请选择要操作的评论",
          type: "warning",
          duration: 1000,
        });
        return;
      }
      let uqids = [];
      for (let i = 0; i < this.multipleSelection.length; i++) {
        uqids.push(this.multipleSelection[i].uqid);
      }
      this.verifyDialogVisible = true;
      this.verifyForm.uqids = uqids;
    },
  },
};
</script>
<style>
.success-status {
  background-color: #67c23a;
  color: white;
}
.error-status {
  background-color: #f56c6c;
  color: white;
}
</style>


